package org.example.mq.mqserver.datacenter;

import lombok.extern.slf4j.Slf4j;
import org.example.mq.mqserver.core.Bind;
import org.example.mq.mqserver.core.Exchange;
import org.example.mq.mqserver.core.Message;
import org.example.mq.mqserver.core.MessageQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

@Slf4j
@Component
public class DiskDataCenter {
    @Autowired
    private MessageFileManager messageFileManager;
    @Autowired
    private DataBaseManager dataBaseManager;

    public void init() {
        dataBaseManager.init();
        MessageFileManager.init();
    }

    public void closeInit() throws InterruptedException {
        dataBaseManager.closeInit();
    }

    // ------------------- Exchange 操作 -------------------

    public boolean insertExchange(Exchange exchange) {
        return dataBaseManager.insertExchange(exchange);
    }

    public boolean deleteExchange(String name) {
        return dataBaseManager.deleteExchange(name);
    }

    public List<Exchange> queryAllExchange() {
        return dataBaseManager.queryAllExchange();
    }

    // ------------------- Queue 操作 -------------------
    public boolean insertMessageQueue(MessageQueue messageQueue){
        try {
            if (messageQueue.isDurable()) {
                messageFileManager.createQueueFiles(messageQueue.getName());
                dataBaseManager.insertMessageQueue(messageQueue);
            }
            return true;
        }catch (Exception e){
            System.out.println("[DiskDataCenter]插入队列:"+messageQueue.getName()+"失败!");
            e.printStackTrace();
            return false;
        }
    }

    public boolean deleteMessageQueue(String name){
        try{
            messageFileManager.delQueueFiles(name);
            dataBaseManager.deleteMessageQueue(name);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return  false;
        }
    }

    public List<MessageQueue> queryAllMessageQueue() {
        return dataBaseManager.queryAllMessageQueue();
    }

    // ------------------- Bind 操作 -------------------
    public boolean insertBind(Bind bind) {
        return dataBaseManager.insertBind(bind);
    }

    public boolean deleteBind(String exchangeName,String queueName,String bindingKey) {
        return dataBaseManager.deleteBind(exchangeName,queueName,bindingKey);
    }

    public List<Bind> queryBindByExchange(String exchangeName){
        return dataBaseManager.queryBindByExchange(exchangeName);
    }

    public List<Bind> queryBindByQueue(String queueName){
        return dataBaseManager.queryBindByQueue(queueName);
    }

    public List<Bind> queryAllBind(){
        return dataBaseManager.queryAllBind();
    }

    // ------------------- Message 持久化操作 -------------------

    public void writeMessageToDisk(MessageQueue queue, Message message) throws IOException {
        messageFileManager.sendMessage(queue, message);
    }

    public LinkedList<Message> loadAllMessageFromQueue(String queueName) throws IOException {
        return messageFileManager.loadAllMessageFromQueue(queueName);
    }


    public void deleteMessage(MessageQueue queue,Message message) {
        messageFileManager.delMessage(queue,message);
    }

    public void messageGc(MessageQueue queue) throws IOException {
        messageFileManager.messageGc(queue);
    }

}
